/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2019-02-13 Time: 22:58
 */

import {AsyncStorage} from 'react-native';

const kUser = 'kUser';
const UserCache = 'UserCache';
const UserAlinarg = 'UserAlinarg';
const UserButtonRight = 'UserButtonRight';
const UserLoginRight = 'UserLoginRight';

export default class User {

  /**
   * 保存用户信息
   * @param callBack
   * @param token
   * @param onLogin
   */
  static saveUser = (token,onLogin, callBack) => {
    console.log('save User ...');

    let user = {
        token:token
    };

    Object.assign(user,onLogin);

    AsyncStorage.setItem(kUser, JSON.stringify(user)).then(res => {
      callBack(res)
    })
  }

    /**
     * 保存字典
     * @param cache
     */
    static saveCache = (cache, callBack,) => {
        console.log('save Cache ...')

        let userCache = {
            cache: cache,
        }

        AsyncStorage.setItem(UserCache, JSON.stringify(userCache)).then(res => {
            callBack(res)
        })
    }

    static saveAlinarg = (alinarg, callBack,) => {
        console.log('save alinarg ...')

        let userAlinarg = {
            alinarg: alinarg,
        }

        AsyncStorage.setItem(UserAlinarg, JSON.stringify(userAlinarg)).then(res => {
            callBack(res)
        })
    }

    static saveButtonRight = (buttonRight, callBack,) => {
        console.log('save buttonRight ...')

        let userButtonRight = {
            buttonRight: buttonRight,
        }

        AsyncStorage.setItem(UserButtonRight, JSON.stringify(userButtonRight)).then(res => {
            callBack(res)
        })
    }

    static saveLoginRight = (loginRight, callBack,) => {
        console.log('save loginRight ...')

        let userLoginRight = {
            loginRight: loginRight,
        }

        AsyncStorage.setItem(UserLoginRight, JSON.stringify(userLoginRight)).then(res => {
            callBack(res)
        })
    }







  /**
   * 清空用户信息
   */
  static clear = () => {
    AsyncStorage.removeItem(kUser);
  };


  /**
   * 获取用户信息
   * @param user
   */
  static getUser = (user) => {
    AsyncStorage.getItem(kUser)
    .then((value) => {
      if (value === null) {
        user(false);
      } else {
        user(JSON.parse(value));
      }
    })
  };

  /**
   * 获取用户本地缓存
   * @param cache
   */
  static getCache = (cache) => {
      AsyncStorage.getItem(UserCache)
          .then((value) => {
              if (value === null) {
                  cache(false);
              } else {
                  cache(JSON.parse(value));
              }
          })
  };

    static getAlinarg = (alinarg) => {
        AsyncStorage.getItem(UserAlinarg)
            .then((value) => {
                if (value === null) {
                    alinarg(false);
                } else {
                    alinarg(JSON.parse(value));
                }
            })
    };


    static getButtonRight = (buttonRight) => {
        AsyncStorage.getItem(UserButtonRight)
            .then((value) => {
                if (value === null) {
                    buttonRight(false);
                } else {
                    buttonRight(JSON.parse(value));
                }
            })
    };

    static getLoginRight = (loginRight) => {
        AsyncStorage.getItem(UserLoginRight)
            .then((value) => {
                if (value === null) {
                    loginRight(false);
                } else {
                    loginRight(JSON.parse(value));
                }
            })
    };

}